using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using EasyCommerce.BLL;
using EasyCommerce.DAL;
using System.Data.SqlTypes;
using CodeWork.Library;
using CodeWork.Library.Web;

public partial class admin_DiscountCouponEdit : BasePage
{
    private SecurityLevel _securityLevel = SecurityLevel.One;

    protected void Page_Load(object sender, EventArgs e)
    {
        Title = _siteName;

        if (!IsPostBack)
        {
            if (Request["id"] != null)
                LoadData();
        }
    }

    private void LoadData()
    {
        int id = 0;
        DiscountCoupon discountCoupon;

        if (Int32.TryParse(Request["id"], out id))
            discountCoupon = DiscountCoupon.Load(id);
        else
            discountCoupon = null;

        if (discountCoupon != null)
        {
            if (!string.IsNullOrEmpty(discountCoupon.CouponName))
				tbxCouponName.Text = discountCoupon.CouponName;
			if (!string.IsNullOrEmpty(discountCoupon.CouponCode))
				tbxCouponCode.Text = discountCoupon.CouponCode;
			tbxCouponDiscountPrice.Text = discountCoupon.CouponDiscountPrice.ToString();
			if (!string.IsNullOrEmpty(discountCoupon.CouponImageUrl))
				tbxCouponImageUrl.Text = discountCoupon.CouponImageUrl;
			if (!string.IsNullOrEmpty(discountCoupon.CouponDescription))
				tbxCouponDescription.Text = discountCoupon.CouponDescription;
			cbxIsActive.Checked = discountCoupon.IsActive;
			if (discountCoupon.CreateDate != SqlDateTime.MinValue)
				tbxCreateDate.Text = discountCoupon.CreateDate.ToShortDateString();
			if (discountCoupon.ExpiryDate != SqlDateTime.MinValue)
				tbxExpiryDate.Text = discountCoupon.ExpiryDate.ToShortDateString();

            ViewState["id"] = id;
        }
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        string couponName = Security.BindStringInput(tbxCouponName.Text, _securityLevel);
		string couponCode = Security.BindStringInput(tbxCouponCode.Text, _securityLevel);
		double couponDiscountPrice = Security.BindDoubleInput(tbxCouponDiscountPrice.Text, _securityLevel);
		string couponImageUrl = Security.BindStringInput(tbxCouponImageUrl.Text, _securityLevel);
		string couponDescription = Security.BindStringInput(tbxCouponDescription.Text, _securityLevel);
		bool isActive = cbxIsActive.Checked;
		DateTime createDate = Security.BindDateTimeInput(tbxCreateDate.Text, _securityLevel);
		DateTime expiryDate = Security.BindDateTimeInput(tbxExpiryDate.Text, _securityLevel);

        if (ViewState["id"] != null)
        {
            int id = 0;
            if (int.TryParse(ViewState["id"].ToString(), out id))
            {
                if (DiscountCoupon.Save(
				id, 
				couponName,
				couponCode,
				couponDiscountPrice,
				couponImageUrl,
				couponDescription,
				isActive,
				createDate,
				expiryDate
                ))
                    Response.Redirect("discountcouponlist.aspx");
                else
                    ResponseMessage = "Save failed";
            }
            else
                ResponseMessage = "Save failed";
        }
        else
        {
            DiscountCoupon discountCoupon = DiscountCoupon.Create(
				couponName,
				couponCode,
				couponDiscountPrice,
				couponImageUrl,
				couponDescription,
				isActive,
				createDate,
				expiryDate
                );

            if (discountCoupon != null)
                Response.Redirect("discountcouponlist.aspx");
            else
                ResponseMessage = "Failed to create user";
        }
    }

    protected void Back_Click(object sender, EventArgs e)
    {
        Response.Redirect("discountcouponlist.aspx");
    }
}
